package com.building.controller;/**
 * Created by LH on 2021/4/7.
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.building.common.R;
import com.building.controller.BaseController.BaseController;
import com.building.entity.User;
import com.building.enums.REnum;
import com.building.util.CheckSumUtil;
import com.building.util.JwtUtil;
import com.building.util.MD5Utils;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.UUID;

/**
 * @ClassName PublicController
 * @Description: TODO
 * @Author LH
 * @Date 2021/4/7 
 **/
@RestController
@RequestMapping("public")
public class PublicController extends BaseController {

    @PostMapping("senCode/{userPhone}")
    public R senCode(@PathVariable("userPhone") String userPhone) throws Exception {
        Calendar calendar = Calendar.getInstance();
        String date = calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DAY_OF_MONTH);
        Integer codeCount = (Integer) redisUtil.getString(userPhone + "_" + date);
        if (codeCount != null && codeCount >= 3) {
            return R.result(REnum.ERROR_CODECOUNT);
        }
        if (codeCount == null) {
            codeCount = 0;
        }

        String codeRedis = (String) redisUtil.getString(userPhone);
        if (codeRedis == null) {
            String codeMes = CheckSumUtil.sendMessage(userPhone);
            redisUtil.setString(userPhone, codeMes, 120);
            redisUtil.setString(userPhone + "_" + date, ++codeCount);
            return R.result(REnum.SUCCESS_CODE);
        }

        return R.result(REnum.ERROR_CODE);
    }

    @PostMapping("login")
    public R login(@RequestBody User user){
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("user_name",user.getUserName());
        User one = userService.getOne(queryWrapper);
        if(one != null){
            if(!one.getUserName().equals(user.getUserName())){
                return R.result(REnum.ERROR_LOGIN);
            }
            if(!one.getUserPassword().equals(MD5Utils.MD5Lower(user.getUserPassword(),one.getUserSalt()))){
                return R.result(REnum.ERROR_LOGIN);
            }
        }
        return R.result(REnum.SUCCESS_LOGIN,user.getUserType());
    }

    @PostMapping("register")
    public R register(@RequestBody User user) throws Exception {
        String  code = (String)redisUtil.getString(user.getUserPhone());
        if(code != null){
            if(user.getCode().equals(code)){
                UUID uuid = UUID.randomUUID();
                user.setUserSalt(user.toString());
                user.setUserPassword(MD5Utils.MD5Lower(user.getUserPassword(),uuid.toString()));
                userService.save(user);
                String token = JwtUtil.createToken(user.getUserId(), user.getUserPhone());
                return R.result(REnum.SUCCESS_REGISTER,token);
            }
        }
        return R.result(REnum.ERROR_CODE);
    }
}
